package com.atguigu.ggkt.vod.controller;


import com.atguigu.ggkt.exception.GgktException;
import com.atguigu.ggkt.model.vod.Teacher;
import com.atguigu.ggkt.result.Result;
import com.atguigu.ggkt.vo.vod.TeacherQueryVo;
import com.atguigu.ggkt.vod.service.TeacherService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author Jetwang
 * @since 2022-06-29
 */
@RestController
@RequestMapping("/admin/vod/teacher")
@Api(tags = "讲师管理接口")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    @ApiOperation("根据id查询")
    @GetMapping("teacher/getTeacher/{id}")
    public Teacher getTeacherLive(@PathVariable Long id) {
        Teacher teacher = teacherService.getById(id);
        return teacher;
    }

    /**
     * 查询所有讲师
     * @return
     */
    @GetMapping("teachers")
    @ApiOperation("查询所有讲师")
    public Result listTeachers() {
        List<Teacher> teachers = teacherService.list();
        return Result.ok(teachers);
    }

    /**
     * 根据id删除讲师
     * @param id
     * @return
     */
    @ApiOperation("根据id删除讲师")
    @DeleteMapping("teacher/{id}")
    public Result deleteTeacherById(@PathVariable Long id) {
        teacherService.removeById(id);
        return Result.ok();
    }


    /**
     * 条件查询分页
     * @param current 当前页码
     * @param limit 页数大小
     * @param teacherQueryVo 查询条件
     * @return
     */
    @ApiOperation("条件查询分页")
    @PostMapping("teachers/{current}/{limit}")
    public Result findPage(@PathVariable Long current,
                           @PathVariable Long limit,
                           @RequestBody(required = false) TeacherQueryVo teacherQueryVo) {
        Page<Teacher> page = new Page<>(current, limit);
        // 1.判断teacherQueryVo是否为空
        if (teacherQueryVo == null) {
            teacherService.page(page, null);
        } else {
            // 2.获取条件值，进行非空判断,条件封装
            String name = teacherQueryVo.getName();
            Integer level = teacherQueryVo.getLevel();
            String joinDateBegin = teacherQueryVo.getJoinDateBegin();
            String joinDateEnd = teacherQueryVo.getJoinDateEnd();
            LambdaQueryWrapper<Teacher> lqw = new LambdaQueryWrapper<>();
            lqw.like(!StringUtils.isEmpty(name), Teacher::getName, name);
            lqw.eq(!StringUtils.isEmpty(level), Teacher::getLevel, level );
            lqw.ge(!StringUtils.isEmpty(joinDateBegin), Teacher::getJoinDate, joinDateBegin);
            lqw.le(!StringUtils.isEmpty(joinDateEnd), Teacher::getJoinDate, joinDateEnd);
            // 3.调用方法，分页查询
            teacherService.page(page, lqw);
        }
        return Result.ok(page);
    }

    /**
     * 添加讲师
     * @param teacher
     * @return
     */
    @ApiOperation("添加讲师")
    @PostMapping("teacher")
    public Result saveTeacher(@RequestBody Teacher teacher) {
        boolean isSuccess = teacherService.save(teacher);
        if (!isSuccess) {
            return Result.fail();
        }
        return Result.ok();
    }

    /**
     * 根据id获取讲师
     * @param id
     * @return
     */
    @ApiOperation("根据id获取讲师")
    @GetMapping("teacher/{id}")
    public Result getTeacherById(@PathVariable Long id) {
        Teacher teacher = teacherService.getById(id);
        return Result.ok(teacher);
    }

    /**
     * 修改讲师
     * @param teacher
     * @return
     */
    @ApiOperation("修改讲师")
    @PutMapping("teacher")
    public Result updateTeacher(@RequestBody Teacher teacher) {
        boolean isSuccess = teacherService.updateById(teacher);
        if (!isSuccess) {
            return Result.fail();
        }
        return Result.ok();
    }

    /**
     * 批量删除讲师
     * @param ids
     * @return
     */
    @ApiOperation("批量删除讲师")
    @DeleteMapping("teachers")
    public Result removeBatch(@RequestBody List<Long> ids) {
        boolean isSuccess = teacherService.removeByIds(ids);
        if (!isSuccess) {
            return Result.fail();
        }
        return Result.ok();
    }

}

